<?php

namespace plugins\aliyun;

use cmf\lib\Plugin;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

class AliyunPlugin extends Plugin
{
    public $info = [
        'name' => 'Aliyun',
        'title' => '阿里云直播插件',
        'description' => '阿里云直播插件',
        'status' => 1,
        'author' => 'zyl',
        'version' => '1.1.1',
        'demo_url' => '',
        'author_url' => '',
    ];

    public $hasAdmin = 0; //插件是否有后台管理界面

    // 插件安装
    public function install()
    {
        return true; //安装成功返回true，失败false
    }

    // 插件卸载
    public function uninstall()
    {
        return true; //卸载成功返回true，失败false
    }

    public function getLiveStreamsNotifyUrlConfig()
    {
        $config = $this->getConfig();
        AlibabaCloud::accessKeyClient($config['AccessKeyID'], $config['AccessKeySecret'])
            ->regionId('cn-hangzhou')
            ->asDefaultClient();

        try {
            $result = AlibabaCloud::rpc()
                ->product('live')
                // ->scheme('https') // https | http
                ->version('2016-11-01')
                ->action('DescribeLiveStreamsControlHistory')
                ->method('POST')
                ->host('live.aliyuncs.com')
                ->options([
                    'query' => [
                        'RegionId' => "cn-hangzhou",
                        'StartTime' => "2020-01-18T17:37:00Z",
                        'EndTime' => "2020-02-13T17:37:00Z",
                        'DomainName' => "fangtaitlzb.0513app.cn",
                    ],
                ])
                ->request();
            return $result->toArray();
        } catch (ClientException $e) {
            return $e->getErrorMessage() . PHP_EOL;
        } catch (ServerException $e) {
            return $e->getErrorMessage() . PHP_EOL;
        }
    }
}
